package com.bugbuff.dubbo.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.bugbuff.dubbo.server.service.TaoHuaService;
import com.bugbuff.dubbo.server.vo.VoiceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("peachblossom")
@Slf4j
public class TaoHuaController {

    @Autowired
    private TaoHuaService taoHuaService;

    /**
     * 发送语音
     * @param voiceFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> sendVoice(@RequestParam("soundFile") MultipartFile voiceFile){
        try {
            Boolean result=taoHuaService.sendVoice(voiceFile);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("语音发送失败！");
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping
    public ResponseEntity<VoiceVo> receiveVoice(){
        try {
            VoiceVo voiceVo=taoHuaService.receiveVoice();
            if(ObjectUtil.isNotEmpty(voiceVo)){
                return ResponseEntity.ok(voiceVo);
            }
        } catch (Exception e) {
            log.error("接收语音失败！");
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
